﻿using System;
using System.Collections.Generic;
using Terraria.ModLoader.Exceptions;

namespace Terraria.ModLoader
{
	// Token: 0x02000204 RID: 516
	public static class RecipeHooks
	{
		// Token: 0x060016D5 RID: 5845 RVA: 0x0001171C File Offset: 0x0000F91C
		internal static void Add(GlobalRecipe globalRecipe)
		{
			RecipeHooks.globalRecipes.Add(globalRecipe);
		}

		// Token: 0x060016D6 RID: 5846 RVA: 0x00011729 File Offset: 0x0000F929
		internal static void Unload()
		{
			RecipeHooks.globalRecipes.Clear();
		}

		// Token: 0x060016D7 RID: 5847 RVA: 0x004169A4 File Offset: 0x00414BA4
		internal static void AddRecipes()
		{
			foreach (Mod mod in ModLoader.mods.Values)
			{
				try
				{
					mod.AddRecipes();
					foreach (ModItem modItem in mod.items.Values)
					{
						modItem.AddRecipes();
					}
				}
				catch (Exception inner)
				{
					ModLoader.DisableMod(mod.Name);
					throw new AddRecipesException(mod, "An error occured in adding recipes for " + mod.Name, inner);
				}
			}
		}

		// Token: 0x060016D8 RID: 5848 RVA: 0x00416A68 File Offset: 0x00414C68
		internal static void PostAddRecipes()
		{
			foreach (Mod mod in ModLoader.mods.Values)
			{
				try
				{
					mod.PostAddRecipes();
				}
				catch (Exception inner)
				{
					ModLoader.DisableMod(mod.Name);
					throw new AddRecipesException(mod, "An error occured after adding recipes for " + mod.Name, inner);
				}
			}
		}

		// Token: 0x060016D9 RID: 5849 RVA: 0x00416AEC File Offset: 0x00414CEC
		public static bool RecipeAvailable(Recipe recipe)
		{
			ModRecipe modRecipe = recipe as ModRecipe;
			if (modRecipe != null && !modRecipe.RecipeAvailable())
			{
				return false;
			}
			foreach (GlobalRecipe globalRecipe in RecipeHooks.globalRecipes)
			{
				if (!globalRecipe.RecipeAvailable(recipe))
				{
					return false;
				}
			}
			return true;
		}

		// Token: 0x060016DA RID: 5850 RVA: 0x00416B58 File Offset: 0x00414D58
		public static void OnCraft(Item item, Recipe recipe)
		{
			ModRecipe modRecipe = recipe as ModRecipe;
			if (modRecipe != null)
			{
				modRecipe.OnCraft(item);
			}
			foreach (GlobalRecipe globalRecipe in RecipeHooks.globalRecipes)
			{
				globalRecipe.OnCraft(item, recipe);
			}
		}

		// Token: 0x040015B2 RID: 5554
		internal static readonly IList<GlobalRecipe> globalRecipes = new List<GlobalRecipe>();
	}
}
